#include "GameMain.h"

heroMediator *heroMediator::initCreate()
{
	heroMediator *hero = NULL;
	do
	{
		hero = heroMediator::node();
		CC_BREAK_IF(!hero);
	}while(0);
	GameMain::sharedGameMain()->getHeroLayer()->addChild(hero);
	return hero;
}

bool heroMediator::init()
{
	bool flag = false;
	do
	{
		CC_BREAK_IF(! CCSprite::init());
		this->schedule(schedule_selector(heroMediator::update),1,kCCRepeatForever,0.0f);
		flag = true;
	}while(0);
	return flag;
}

heroModel *heroMediator::getModel()
{
	return model;
}

void heroMediator::setModel(heroModel *value)
{
	model = value;
	model->setView(this);
}

void heroMediator::update(float dt)
{
	if(model->getWay().size() != 0)
		heroMediator::way();
	else
	{
		/*vector<buildMediator *> arr = GameDataViewManager::sharedGameDataViewManager()->getBuild();
		float range = 0.0f;
		float angle;
		for (int i = 0; i < arr.size(); i++)
		{
			buildModel *buildmodel = arr.at(i)->getModel();
			CCPoint point = GameMain::sharedGameMain()->getBgLayer()->getViewPoint(buildmodel->getCurrentX(),buildmodel->getCurrentY());
			float currentAngle = atan((point.y - model->getCurrentY()) / (point.x - model->getCurrentX()));
			float currentRange = (point.y - model->getCurrentY()) / sin(currentAngle);
			if(range == 0.0f)
			{
				range = currentRange;
				angle = currentAngle;
			}
			else if(range > currentRange)
			{
				range = currentRange;
				angle = currentAngle;
			}
		}
		for (int j = range / model->getSpeed(); j > 0 ; j--)
		{
			vector<float> way;
			way.push_back(model->getCurrentX() + model->getSpeed() * j * cos(angle));
			way.push_back(model->getCurrentY() + model->getSpeed() * j * sin(angle));
			model->setWay(way);
			model->getWay().pop_back();
		}
		heroMediator::way();*/
	}
}

void heroMediator::dataChange(bool value)
{
	if(value)
	{
		this->initWithTexture(GameLoader::sharedGameLoader()->getTexture2d("CloseNormal.png"));
		this->setPosition(ccp(model->getCurrentX(),model->getCurrentY()));
	}
	else
	{
		CCMoveTo * move = CCMoveTo::create(1,ccp(model->getCurrentX(),model->getCurrentY()));
		this->runAction(move);
	}
}

void heroMediator::way()
{
	if(model->getWay().size() != 0)
	{
		vector<float> waying = model->getWay().at(model->getWay().size() - 1);
		model->setCurrentX(waying.at(0));
		model->setCurrentY(waying.at(1));
		model->removeWay();
		model->setChange();
	}
}

void heroMediator::stand()
{
	CCAnimate *animate = GameOther::getAnimate(model->getId(), "stand.png");
	this->runAction(CCSequence::create(animate));
}

void heroMediator::run()
{
	CCAnimate *animate = GameOther::getAnimate(model->getId(),"run.png");
	this->runAction(CCSequence::create(animate));
}

void heroMediator::attack()
{
	CCAnimate *animate = GameOther::getAnimate(model->getId(),"attack.png");
	this->runAction(CCSequence::create(animate,callfunc_selector(heroMediator::end),NULL));
}

void heroMediator::skill()
{
	CCAnimate *animate = GameOther::getAnimate(model->getId(),"skill.png");
	this->runAction(CCSequence::create(animate,callfunc_selector(heroMediator::end),NULL));
}

void heroMediator::die()
{
	CCAnimate *animate = GameOther::getAnimate(model->getId(),"die.png");
	this->runAction(CCSequence::create(animate,callfunc_selector(heroMediator::end),NULL));
}

void heroMediator::end()
{
	this->getActionManager()->removeAllActions();
	heroMediator::stand();
}
